package _02_linked_list;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Consumer;

/**
 * @author mornd
 * 自定义单向链表<Integer>
 */
public class SinglyLinkedList implements Iterable<Integer> {
    //头指针
    private Node head;

    public void addFirst(int e) {
        head = new Node(e, head);
    }

    public void add(int e) {
        addLast(e);
    }

    public int get(int index) {
        return getNode(index).value;
    }

    private Node getNode(int index) {
        Node temp = head;
        for (int i = 0; i < index; i++) {
            temp = temp.next;
        }
        return temp;
    }

    public Node getLast() {
        Node p = head;
        if(p==null) {
            return p;
        }
        while (p.next != null) {
            p = p.next;
        }
        return p;
    }

    private void removeFirst() {
        if(head != null) {
            head = head.next;
        } else {
            throw new NoSuchElementException("index [0]");
        }
    }

    public void remove(int index) {
        if(index < 0) {
            throw new IllegalArgumentException(String.format("index %d Illegal", index));
        }
        if(index == 0) {
            removeFirst();
        } else {
            Node prev = getNode(index - 1);
            Node node = prev.next;
            prev.next = node.next;
        }
    }

    public void insert(int index, int e) {
        if(index < 0) {
            throw new IllegalArgumentException(String.format("index %d Illegal", index));
        }
        if(index == 0) {
            addFirst(e);
        } else {
            Node prev = getNode(index - 1);
            prev.next = new Node(e, prev.next);
        }
    }

    public void addLast(int e) {
        Node newNode = new Node(e, null);
        if(head == null) {
            head = newNode;
        } else {
            Node lastNode = getLast();
            lastNode.next = newNode;
        }
    }

    public void loop(Consumer<Integer> before, Consumer<Integer> after) {
        recursion(before, after, head);
    }

    private void recursion(Consumer<Integer> before, Consumer<Integer> after, Node node) {
        if(node == null) {
            return;
        }

        before.accept(node.value);
        recursion(before, after, node.next);
        after.accept(node.value);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        for (Node p = head; p != null; p = p.next) {
            sb.append(p.value);
            if(p.next != null) {
                sb.append(",");
            }
        }
        return sb.append("]").toString();
    }

    /**
     * 返回迭代器
     * @return
     */
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            private Node p = head;
            @Override
            public boolean hasNext() {
                return p != null;
            }

            @Override
            public Integer next() {
                Node r = p;
                this.p = r.next;
                return r.value;
            }
        };
    }

    public void foreach(Consumer<Integer> c) {
        Node p = head;
        while (p != null) {
            c.accept(p.value);
            p = p.next;
        }
    }

    /**
     * 节点元素
     */
    private static class Node {
        int value;
        Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }
}
